using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Gencode.Common.SQL;

namespace Personalplaner
{
    public class Verwaltung
    {
        static int year;
        static int yearID;
        static string m_mandid;
        static string m_user;

        public Verwaltung()
        {
            Initialize();
        }

        /// <summary>
        /// Initializes statics
        /// </summary>
        static public bool Initialize()
        {
			bool noError = true;
			Gencode.Common.ActionLogger.Logger.LogName = "prslog.log";
			Gencode.Common.ActionLogger.Logger.UseLogger = true;

			if (noError)
				noError = SetYear();

			return noError;
        }

		/// <summary>
		/// cleans all statics
		/// </summary>
		public static void Dispose()
		{
			Gencode.Common.ActionLogger.Logger.Dispose();
		}

        static public bool SetYear()
        {
			bool noError = true;

            //Year
			DataView dv = SimpleQuerry.ExecuteForDataView("select JahrID, Jahr from PlanungsJahr where Status = 1 and mandid = '" + Mandant + "'");
            try
            {
                yearID = Int32.Parse(dv[0].Row[0].ToString());
                year = Int32.Parse(dv[0].Row[1].ToString());
            }
            catch (Exception exp)
            {
				Gencode.Common.Util.ErrorHandler.LoggError("Verwaltung Error", "Verwaltung.setYear()", exp.Message);
				Gencode.Common.ActionLogger.Logger.LoggMessage(Gencode.Common.ActionLogger.MessageType.DATA_ERROR, "No year could be set for mandant '" + Mandant + "'");
				noError = false;
            }

			return noError;
        }

        static public int Year
        {
            get { return year; }
            set { year = value; }
        }

        static public int YearID
        {
            get
            {
                return yearID;
            }
        }

        public static string Mandant
        {
            get
            {
                if (m_mandid == null || m_mandid == string.Empty)
                    m_mandid = Gencode.PrsCommon.Data.Common.CommonData.GetMandant(User);
                return m_mandid;
            }
        }

        public static string User
        {
            get
            {
                if (m_user == null || m_user == string.Empty)
                    m_user = Properties.Settings.Default.User;
                        //System.Configuration.ConfigurationManager.AppSettings.Get("User");
                return m_user;
            }
        }

		static string shortVersion;
		public static string ProductVersionShort
		{
			get
			{
				if (string.IsNullOrEmpty(shortVersion))
				{
					shortVersion = System.Windows.Forms.Application.ProductVersion;
					string[] version = System.Windows.Forms.Application.ProductVersion.Split('.');
                    shortVersion = version[0] + "." + version[1];// +"." + version[2];
				}
				return shortVersion;
			}
		}

        public static string ProductVersionBuild
        {
            get
            {
                    string[] version = System.Windows.Forms.Application.ProductVersion.Split('.');
                    return version[2];
            }
        }

		public static string ProductVersion
		{
			get
			{
				return System.Windows.Forms.Application.ProductVersion;
			}
		}

		public static string ProductName
		{
			get
			{
                return System.Windows.Forms.Application.ProductName + " v" + ProductVersionShort;// +"." + ProductVersionBuild;
			}
		}
    }
}
